cc.Class({
    extends: cc.Component,
	
	ctor: function (game) {
		
		this.game = game;
		
		this.moveBlockNode=[];
		
		//触发爆破
		this.boomCallback  = cc.callFunc(function () {
				this.boomBlock();
		}, this);
		
		//触发下一次特效
		this.effectCallback = cc.callFunc(function () {
				this.playEffect();
		}, this); 
		
		//触发创建新球
		this.createNewCallback = cc.callFunc(function () {
				this.createNew();
		}, this); 
		
		//触发继续游戏
		this.goonCallback = cc.callFunc(function () {
				this.goon();
		}, this); 
		
		//触发道具移除
		this.removeBlockNodeCallBack = cc.callFunc(function () {
				this.removeBlockNode();
		}, this); 
		
		//触发检查是否有获取道具
		this.getItemCallback = cc.callFunc(function () {
				this.getItem();
		}, this);
		
		//触发爆炸效果移除
		this.removeCallback = cc.callFunc(function () {
				this.removeBlock();
		}, this);
		
	},
	
	disappearOneBlock(block){
		
		if(block.iceNode!=null){
			this.game.node.removeChild(block.iceNode);
			this.game.effectTop.addChild(block.iceNode);
		}
		
		
		if(block.blockNode!=null&&block.blockNode.parent == this.game.node){
			this.game.node.removeChild(block.blockNode);
			this.game.effectTop.addChild(block.blockNode);
		}
		
		
		
		if(block.type2==1){
			if(block.blockNode!=null)
				block.blockNode.runAction(cc.scaleTo(0.3,this.game.blockScale*0.5,this.game.blockScale*0.5));
		}else{
			
			if(block.effectFlag==0){
				block.effectFlag=1;
				this.game.mapPointForEffectArr.push(block);
			}
			
		}
	},
	
	disappear(){

		var arr = this.game.mapPointForDisappearArr;

		for(var i=0;i<arr.length;i++){
			var block = arr[i];
			this.disappearOneBlock(block);
		}
		
		if(this.game.mapPointForEffectArr.length>0){
			this.game.effectMast.active = true;
			this.game.node.runAction(cc.sequence(cc.delayTime(0.3),this.effectCallback));
		}else{
			this.game.node.runAction(cc.sequence(cc.delayTime(0.3),this.boomCallback));
		}
		
	},
	
	removeAllEffect(){
		for(var i=0;i<this.game.effectArr.length;i++){
			this.game.effectTop.removeChild(this.game.effectArr[i]);
		}
		this.game.effectArr=[];
	},
	
	playEffect(){
		
		this.removeAllEffect();
		
		
		if(this.game.mapPointForEffectArr.length>0){
			
			var effectBlock = this.game.mapPointForEffectArr[0];
			
			var waittime = this.game.effectManager.createEffect(effectBlock,this.game);
			
			var newAddBlock = this.game.effectManager.getNewAddBlock(effectBlock,this.game);
			
			for(var i=0;i<newAddBlock.length;i++){
				
				var block = newAddBlock[i];
				
				if(this.game.pushMapPointForDisappear(block)){
					this.disappearOneBlock(block);
				}
				
			}

			this.game.mapPointForEffectArr.splice(0,1);
			
			this.game.node.runAction(cc.sequence(cc.delayTime(waittime),this.effectCallback));
		}else{
			this.boomBlock();
		}
	},
	
	
	boomBlock(){
		
		
		var arr = this.game.mapPointForDisappearArr;
		
		this.game.scoreadd += 10*arr.length;
		
		for(var i=0;i<arr.length;i++){
			var block = arr[i];
			this.game.effectManager.createBoomEffect(block,this.game);
			if(this.game.playerType!=3){
				this.game.effectTop.removeChild(block.blockNode);
			}else{
				if(
					(this.game.targetType2==8&&block.type2==8)||
					(block.type1 == this.game.targetType1 && block.type2 == this.game.targetType2)||
					(this.game.targetType1==-1&&block.type2==this.game.targetType2)
				){
					this.moveBlockNode.push(block.blockNode);
					block.blockNode.zIndex=200;
					block.blockNode.runAction(cc.sequence(
					cc.scaleTo(0.1,this.game.blockScale*0.5,this.game.blockScale*0.5),
					cc.moveTo(0.4,this.game.targetSign.getPosition())
					));
					
				}else{
					
					//消除冰块
					if(block.iceNode!=null){
						this.moveBlockNode.push(block.iceNode);
						block.iceNode.zIndex=200;
						block.iceNode.runAction(cc.sequence(
						cc.scaleTo(0.1,this.game.blockScale*0.5,this.game.blockScale*0.5),
							cc.moveTo(0.4,this.game.targetSign.getPosition())
						));
						block.iceNode=null;
					}
					
					this.game.effectTop.removeChild(block.blockNode);
				}
			}
			block.blockNode=null;
		}
		
		this.clearMapForDisapper();
		
		this.game.node.runAction(cc.sequence(cc.delayTime(0.5),this.removeCallback));
		
		this.createNew();
		
	},
	
	removeBlock(){
		for(var i=0;i<this.game.boomArr.length;i++){
			this.game.effectTop.removeChild(this.game.boomArr[i]);
		}
		
		if(this.game.playerType==3){
			this.game.completeAdd+=this.moveBlockNode.length;
			this.game.refreshLog();
		}
		
		for(var i=0;i<this.moveBlockNode.length;i++){
			this.game.effectTop.removeChild(this.moveBlockNode[i]);
		}
		
		this.game.boomArr=[];
		this.moveBlockNode=[];
		
		//this.createNew();
	},
	
	clearMapForDisapper(){
		this.game.mapPointNoMap={};
		this.game.mapPointForDisappearArr=[];
		for(var i=0;i<this.game.mapPointArr.length;i++){
			this.game.mapPointArr[i].effectFlag=0;
		}
	},
	
	//重置newNmb
	refreshNewNmb(){
		for(var i=this.game.mapPointArr.length-1;i>=0;i--){
			var block = this.game.mapPointArr[i];
			block.newNmb=0;
		}
	},
	
	//补齐空缺方块
	createNew(){
		
		this.game.effectMast.active=false;
		
		var moveBlockArr=[];
		
		var disappearBlockArr=[];
		
		for(var i=this.game.mapPointArr.length-1;i>=0;i--){
			
			var block = this.game.mapPointArr[i];
				
				if(block.blockNode==null){
					
					disappearBlockArr.push(block);
				
					var up = block;
					
					
					while(up!=null&&up.blockNode==null){
						
						block.movePoints.push(up.mapNode.getPosition());
						
						up = up.mpU;
					}
			
					if(up==null){
						
						var topMp = block.topMp();
						
						topMp.newNmb++;
						
						if(this.game.playerType==2&&!this.hasItem()&&!this.hasItemByBlock(block)&&Math.random()>0.95){
							block.type1=7;
						}else{
							block.type1 = Math.floor(Math.random()*this.game.shapeNmb);
						}
							
						for(var k =1;k<topMp.newNmb;k++){
									
							block.movePoints.push({x:topMp.mapNode.getPosition().x,
												   y:topMp.mapNode.getPosition().y+k*this.game.h});	
							
						}
					
						block.type2=1;
						block.blockNode=this.game.createNode(topMp.x,
															 topMp.y+block.topMp().newNmb*this.game.h,
															 this.game.blockPointFrameArr[block.type1][block.type2],
															 this.game.blockScale);
						moveBlockArr.push(block);
						
					}else{
					
						block.type1 = up.type1;
						block.type2 = up.type2;
						block.blockNode = up.blockNode;
						up.blockNode=null;
					
						moveBlockArr.push(block);
					}
				}
			
		}
		
		
		this.refreshNewNmb();
		
		var moveTime = this.newAnimate(moveBlockArr);
		
		var nextArr=[];
		
		for(var i=0;i<disappearBlockArr.length;i++){
			
			var disappearBlock = disappearBlockArr[i];
			var findresult = disappearBlock.findSame2();

			
			
			if(findresult.type>0){
				
					nextArr.push(findresult.arr);
					
					
					if(findresult.type>1){
						if(disappearBlock.type2==1)
							disappearBlock.waitChangeType = findresult.type;
						nextArr.push([[disappearBlock]]);
					}
					
					for(var j=0;j<findresult.arr.length;j++){
						
						var arrj = findresult.arr[j];
						
						for(var k=0;k<arrj.length;k++){
							if(arrj[k].waitChangeType>findresult.type){
								disappearBlock.waitChangeType=1;
							}else{
								arrj[k].waitChangeType=1;
							}
						}
						
					}
			}
			
		}
		
		if(nextArr.length>0){
			
			var createNewMoveOut = cc.callFunc(function () {
				this.removeSameBlock(nextArr);
				this.game.disappear();
			}, this); 

			this.game.node.runAction(cc.sequence(cc.delayTime(moveTime+0.5), createNewMoveOut));
			
		}else{
			
			this.game.node.runAction(cc.sequence(cc.delayTime(moveTime+0.6), this.getItemCallback));
		}
	},
	
	
	//检查是否有获得道具 如果有则道具掉落  如果没有则游戏继续
	getItem(){
		
		var flag = false;
		
		for(var i=0;i<this.game.mapPointArr.length;i++){
			var mp = this.game.mapPointArr[i];
			if(mp.isboom()&&mp.type1>6){
				flag = true;
				this.game.blockNodeForRemove.push({node:mp.blockNode,type:mp.type2});
				mp.blockNode.runAction(cc.moveBy(0.1,0,-this.game.h));
				mp.blockNode=null;
			}
		}
		
		if(flag){
			this.game.node.runAction(cc.sequence(cc.delayTime(0.1),this.removeBlockNodeCallBack));
			//this.game.node.runAction(cc.sequence(cc.delayTime(0.1),this.createNewCallback));
		}else{
			this.game.goon();
		}
		
	},
	
	//道具掉落后执行
	removeBlockNode(){
		
		var stepadd = 0;
		
		if(this.game.playerType==1){
		
			for(var i=0;i<this.game.blockNodeForRemove.length;i++){
				this.game.node.removeChild(this.game.blockNodeForRemove[i].node);
				if(this.game.blockNodeForRemove[i].type==1){
					stepadd+=5;
				}
			}
			this.game.showMessage('步数 + '+stepadd);
			
			this.createNew();
			
		}else if(this.game.playerType==2){
			
			for(var i=0;i<this.game.blockNodeForRemove.length;i++){
				this.game.node.removeChild(this.game.blockNodeForRemove[i].node);
				if(this.game.blockNodeForRemove[i].type==1){
					stepadd+=500;
				}
			}
		
			this.game.showMessage('时间 + '+stepadd/100);
			
			this.createNew();
			
		}else if(this.game.playerType==3){
			
			var itemflag = false;
			
			
			for(var i=0;i<this.game.blockNodeForRemove.length;i++){
				
				
				if(this.game.targetType1==7 && this.game.blockNodeForRemove[i].type == this.game.targetType2){
					
					this.moveBlockNode.push(this.game.blockNodeForRemove[i].node);
					this.game.blockNodeForRemove[i].node.zIndex=200;
					this.game.blockNodeForRemove[i].node.runAction(cc.sequence(
					cc.scaleTo(0.1,this.game.blockScale*0.5,this.game.blockScale*0.5),
					cc.moveTo(0.4,this.game.targetSign.getPosition())
					));
					
					itemflag=true;
					
				}else{
					this.game.node.removeChild(this.game.blockNodeForRemove[i].node);
				}
				
				if(this.game.blockNodeForRemove[i].type==1){
					stepadd+=5;
				}else if(this.game.blockNodeForRemove[i].type==2){
					this.game.magicNmb++;
				}else if(this.game.blockNodeForRemove[i].type==3){
					this.game.hummerNmb++;
				}
			}
			
			if(stepadd>0){
				this.game.showMessage('步数 + '+stepadd);
			}
			
			if(itemflag){

				this.game.node.runAction(cc.sequence(cc.delayTime(0.5),this.removeCallback));
			
			}else{
				//this.createNew();
			}
			
			this.createNew();
		}
		
		this.game.lastStep+=stepadd;
		
		this.game.blockNodeForRemove=[];
		
	},
	
	newAnimate(moveBlockArr){
		
		var maxTime = 0;
		
		for(var i=0;i<moveBlockArr.length;i++){
			
			var moveTime = moveBlockArr[i].movePoints.length*moveBlockArr[i].moveSpeed;
			
			if(moveTime>maxTime){
				maxTime = moveTime;
			}
			
			moveBlockArr[i].moveByPoints();
		}
		
		return maxTime;
	},
	
	
	removeSameBlock(mapPoints){
		this.clearMapForDisapper();
		var arr = this.game.mapPointForDisappearArr;
		for(var i=0;i<mapPoints.length;i++){
			for(var j=0;j<mapPoints[i].length;j++){
				for(var k=0;k<mapPoints[i][j].length;k++){
					var block = mapPoints[i][j][k];
					var no = block.no;
					if(!this.game.mapPointNoMap[no]){
						this.game.mapPointNoMap[no]=1;
						//arr.push(block);
						if(block.waitChangeType!=1){
							block.resetType2(block.waitChangeType);
							block.waitChangeType=1;
						}else{
							arr.push(block);
						}
					}	
				}	
			}	
		}
		return arr;
	},
	
	pushMapPointForDisappear(block){
		if(block==null){
			return false;
		}
		
		var no = block.no;
		if(!this.game.mapPointNoMap[no]){
			this.game.mapPointNoMap[no]=1;
			this.game.mapPointForDisappearArr.push(block);
			return true;
		}else{
			return false;
		}
	},
	
	hasItemByBlock(block){
		
		var blocku = block.mpU;
		
		while(blocku!=null){
			if(blocku.type1>=7){
				return true;
			}
			blocku=blocku.mpU;
		}
		
		var blockd = block.mpD;
		while(blockd!=null){
			if(blockd.type1>=7){
				return true;
			}
			blockd=blockd.mpD;
		}
		
		return false;
		
	},
	
	hasItem(){
		
		var count = 0;
		
		for(var i=0;i<this.game.mapPointArr.length;i++){
			var mp = this.game.mapPointArr[i];
			if(mp.type1>=7){
				count++;
				if(count>=this.game.maxItemNmb){
					return true;
				}
			}
		}
		return false;
	},
	
	disappearSpe(){
		
		var nmb = 0;
		
		for(var i=0;i<this.game.mapPointArr.length;i++){
			var mp = this.game.mapPointArr[i];
			if(mp.type2>1){
				this.pushMapPointForDisappear(mp);
				nmb++;
			}
		}
		
		if(nmb>0){
			this.disappear();
			return true;
		}else{
			return false;
		}
		
	},
	
	canTouchStart(){
		return this.game.selectedBlock2==null;
	},
	
	canTouchMove(){
		return this.game.selectedBlock!=null&&this.game.selectedBlock2==null;
	}
});
